package com.ganzalang.gmall.threadlocal.interceptor;

import com.ganzalang.gmall.threadlocal.common.util.GlobalParamInfo;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @author: Ganzalang
 * @Date 2021/3/26 15:24
 */
public class ParamInterceptor implements HandlerInterceptor {

    private static final String USERNAME = "USERNAME";
    private static final String USERID = "USERID";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String username = request.getHeader("username");
        String userId = request.getHeader("userId");
        GlobalParamInfo.put(USERNAME, username);
        GlobalParamInfo.put(USERID, userId);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        GlobalParamInfo.clear();
    }
}
